package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.RoomAttrValueMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Slf4j
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /***
     *  根据id获取房间详细信息
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo  roomDetailVo = new RoomDetailVo();

//        根据 房间id 查询房间的基本信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

//        1、根据 房间 id 获取 对应的 公寓信息
       ApartmentInfo  apartmentInfo = roomInfoMapper.selectApartmentInfoByRoomId(id);
        roomDetailVo.setApartmentInfo(apartmentInfo);

//        2、查询 房间的 图片列表
       List<GraphVo>  graphVoList = roomInfoMapper.selectGraphListByRoomId(id);
       roomDetailVo.setGraphVoList(graphVoList);

//        3、查询 房间的属性列表
       List<AttrValueVo>   attrValueVoList = roomInfoMapper.selectAttrValueListByRoomId(id);
       roomDetailVo.setAttrValueVoList(attrValueVoList);

//       4、查询 配套信息列表
       List<FacilityInfo>  facilityInfoList = roomInfoMapper.selectFacilityInfoListByRoomId(id);
       roomDetailVo.setFacilityInfoList(facilityInfoList);

//       5、查询 标签信息列表
        List<LabelInfo>   labelInfoList = roomInfoMapper.selectLabelInfoListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

//       6、查询支付方式列表
        List<PaymentType>  paymentTypeList = roomInfoMapper.selectPaymentTypeByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

//        7、查询可选租期列表
        List<LeaseTerm> leaseTermList = roomInfoMapper.selectRoomLeaseTermByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
       log.info("roomDetailVo:{}" , roomDetailVo);
        return roomDetailVo;
    }

    /***
     *   分页查询 房间的列表
     * @param roomPage
     * @param queryVo
     * @return
     */
    @Override
    public Page<RoomItemVo> selectRoomItemVoPage(Page<RoomItemVo> roomPage, RoomQueryVo queryVo) {

        Page<RoomItemVo> roomItemVoPage = roomInfoMapper.selectRoomItemVoPage(roomPage, queryVo);

        return roomItemVoPage;

    }

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;


    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
//        修改 房间的发布状态
        LambdaUpdateWrapper<RoomInfo> roomInfoWrapper = new LambdaUpdateWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getId, id);
        roomInfoWrapper.set(RoomInfo::getIsRelease, status);

        super.update(roomInfoWrapper);
    }

    @Override
    public void removeRoomById(Long id) {

//        删除 RoomInfo 表中的数据
        super.removeById(id);
//        1、删除 图片
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoWrapper);

//            2、删除 属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper = new LambdaQueryWrapper<>();
        roomAttrValueWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueWrapper);

//            3、删除配套
        LambdaQueryWrapper<RoomFacility> roomFacilityWrapper = new LambdaQueryWrapper<>();
        roomFacilityWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityWrapper);

//            4、删除 标签
        LambdaQueryWrapper<RoomLabel> roomLabelWrapper = new LambdaQueryWrapper<>();
        roomLabelWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelWrapper);

//            5、删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeWrapper);

//            6、删除可选租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermWrapper);
    }

    //     更新 或者 添加 房间信息
    @Override
    public void saveOrUpdateRoomInfoById(RoomSubmitVo roomSubmitVo) {

//        1、判断当前是 添加 还是 修改操作
        boolean isCheckSaveOrUpdate = roomSubmitVo.getId() != null;

//        2、添加或者 修改 房间表中的记录
        super.saveOrUpdate(roomSubmitVo);

        if (isCheckSaveOrUpdate) {
//            代码如果执行到这里，说明是修改操作
//            可以将 房间图片列表、属性列表、配套列表、标签列表、支付方式列表、可选租期列表都进行删除
//            1、删除 图片
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphInfoWrapper);

//            2、删除 属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper = new LambdaQueryWrapper<>();
            roomAttrValueWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueWrapper);

//            3、删除配套
            LambdaQueryWrapper<RoomFacility> roomFacilityWrapper = new LambdaQueryWrapper<>();
            roomFacilityWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityWrapper);

//            4、删除 标签
            LambdaQueryWrapper<RoomLabel> roomLabelWrapper = new LambdaQueryWrapper<>();
            roomLabelWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelWrapper);

//            5、删除支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeWrapper);

//            6、删除可选租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermWrapper);
        }

//        添加
//        1、添加图片列表
//          1.1、从 前端 获取 传递 过来的 图片列表
//        使用 批量删除，将  GraphVo 对象 转换为 Graph对象
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
//            创建  GraphInfo 对象
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


//        2、添加属性列表
//           2.1、从前端获取传递过来的 属性列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
//        3、添加配套信息列表
//            3.1、从前端获取 传递过来的 配套列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

//        4、添加标签列表
//           4.1、从 前端 获取 传递过来的 标签列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            List<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
//            执行方法，完成添加操作
            roomLabelService.saveBatch(roomLabelList);
        }
//        5、添加支付方式列表
//             从前端获取支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            paymentTypeIds.forEach(item -> {
//            创建 RoomPaymentType 对象
                RoomPaymentType roomPaymentType = RoomPaymentType
                        .builder()
                        .roomId(roomSubmitVo.getId())
                        .paymentTypeId(item).build();
                roomPaymentTypeList.add(roomPaymentType);
            });
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

//        6、添加可选租期列表
//         6.1、从 前端 获取 可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .roomId(roomSubmitVo.getId())
                        .leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
//        执行批量插入
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }
}




